import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import person.*;
import organization.*;

import org.junit.Test;


public class teacherTest {

	@Test
	public void testTeacherAdd() {

		// given
		
		Teacher testTeach = new Teacher (new PersonInfo("testAdd","Leroy","Kiev",5,11,1993));
	
		// when
		
		Base.addPerson(testTeach);
		
		// then
		assertEquals (testTeach,Base.getList().getPersonBase().get(0));
		
	}
	@Test
	public void testStudentAdd (){
		
		// given
		
		Student testStud = new Student (new PersonInfo("testAdd","King","Gio",7,12,1995));
		
		// when
		
		Base.addPerson(testStud);
		
		// then
		assertEquals (testStud,Base.getList().getPersonBase().get(Base.getList().getPersonBase().size()-1));
		
	}
	@Test
	public void testAdmAdd(){
		// given
		Admin testAdm = new Admin (new PersonInfo("testAdd","Zina","Kiev",5,8,1990));
		
		// when
		
		Base.addPerson(testAdm);
		
		// then
		
		assertEquals (testAdm,Base.getList().getPersonBase().get(Base.getList().getPersonBase().size()-1));
	}
	@Test
	public void testPersonSort (){
		// given 
		ArrayList <Teacher> inputTeacherList  = new ArrayList <Teacher> ();
		ArrayList <Student> inputStudentList = new ArrayList <Student> ();
		ArrayList <Admin> inputAdminList = new ArrayList <Admin> ();
		
		for (int i=1;i<11;i++){
			if ((i%2) == 0){
				Teacher testTeach = new Teacher (new PersonInfo("Ann"+i,"Leroy"+i,"Kiev",5,11,1993));
				inputTeacherList.add(testTeach);
				Base.addPerson(testTeach);
			}else {
				Student testStudent = new Student (new PersonInfo("Jeorge"+i,"Leroy"+i,"Kiev",5,11,1993));
				inputStudentList.add(testStudent);
				Base.addPerson(testStudent);
			}
			Admin testAdmin = new Admin (new PersonInfo("Ben"+i,"Maggines"+i,"London",6,5,1980));
			inputAdminList.add(testAdmin);
			Base.addPerson(testAdmin);
		}
		
		ArrayList <Teacher> sortedTeacherList  = Base.getSortlist(new Teacher());
		ArrayList <Student> sortedStudentList = Base.getSortlist(new Student());
		ArrayList <Admin> sortedAdmList = Base.getSortlist(new Admin ());
		
		
		for (int i=0;i<inputStudentList.size();i++){
			for (int j=0;j<sortedStudentList.size();j++){
				// when
				if (!inputStudentList.get(i).equals(sortedStudentList.get(j))){
					if (j==(sortedStudentList.size()-1)){
						// then
						fail("Student Failed to find "+inputStudentList.get(i).getFirstAndScndName());
					}
				}else {
					break;
				}
			}
		}
		
		for (int i=0;i<inputAdminList.size();i++){
			for (int j=0;j<sortedAdmList.size();j++){
				// when
				if (!inputAdminList.get(i).equals(sortedAdmList.get(j))){
					if (j==(sortedAdmList.size()-1)){
						// then
						fail("Admin Failed to find "+inputAdminList.get(i).getFirstAndScndName());
					}
				}else {
					break;
				}
			}
		}
		
		for (int i=0;i<inputTeacherList.size();i++){
			for (int j=0;j<sortedTeacherList.size();j++){
				// when
				if (!inputTeacherList.get(i).equals(sortedTeacherList.get(j))){
					if (j==(sortedTeacherList.size()-1)){
						// then
						fail("Teacher Failed to find "+inputTeacherList.get(i).getFirstAndScndName());
					}
				}else {
					break;
				}
			}
		}
	}
	@Test
	public void testAddTeacherSubject (){
		//given
		Teacher test = (Teacher) Base.getSortlist(new Teacher()).get(0);
		
		// when
		for (int i=0;i<5;i++){
			test.addStudySubject("Ma"+i);
		}
		//then
		assertEquals (5,test.getStudySubjectList().size());
	}
	@Test
	public void testGetAudit() {
		//given
		ArrayList<Auditorium> testAudit = new ArrayList<Auditorium>();
		for (int i = 1; i < 15; i++) {
			Auditorium testAuditorium = new Auditorium((i + 10), i);
			testAudit.add(testAuditorium);
			Base.addAudit((i + 10), i);
		}
		for (int i = 0; i < testAudit.size(); i++) {
			//when
			if (Base.getAudObjByNumber(testAudit.get(i).getNumber()) == null) {
				//then
				fail("returned null unexpected " + testAudit.get(i).getNumber());
			}
		}
	}
	@Test
	public void testCheckUniq (){
		//given
		ArrayList <PersonInfo> nonUniqPerson = new ArrayList <PersonInfo> ();
		ArrayList <PersonInfo> uniqPerson = new ArrayList <PersonInfo> ();
		for (int i=0;i<8;i++){
			if (i%2 == 0){
				Teacher testTeach = new Teacher (new PersonInfo("testCheckNonUniq"+i,"Teach","Lon",9,6,1990));
				nonUniqPerson.add(testTeach);
				Base.addPerson(testTeach);
			}else{
				Student testStud = new Student (new PersonInfo("testCheckNonUniq"+i,"Stud","Gio",13,12,1995));
				nonUniqPerson.add(testStud);
				Base.addPerson(testStud);
			}
			Admin testAdm = new Admin (new PersonInfo("testCheckNonUniq"+i,"Adm","Kiev",5,8,1990));
			nonUniqPerson.add(testAdm);
			Base.addPerson(testAdm);
		}
		for (int i=0;i<10;i++){
			if (i%2 == 0){
			uniqPerson.add(new Teacher (new PersonInfo("testCheckUniq"+i,"Teach","Lon",9,6,1990)));
			}else{
				uniqPerson.add(new Student (new PersonInfo("testCheckUniq"+i,"Stud","Gio",13,12,1995)));
			}
			uniqPerson.add(new Admin (new PersonInfo("testCheckUniq"+i,"Adm","Kiev",5,8,1990)));
		}
		//when
		for (int i=0;i<nonUniqPerson.size();i++){
			System.out.println("TEST"+i+" "+nonUniqPerson.get(i).getFirstAndScndName());
			if (Base.checkUniq(nonUniqPerson.get(i))){
				//then
				fail("Failed on Checking non Uniq"+nonUniqPerson.get(i).getFirstAndScndName());
			}
		}
		//when
		for (int i=0;i<uniqPerson.size();i++){
			System.out.println("TEST"+i+" "+uniqPerson.get(i).getFirstAndScndName());
			if (!Base.checkUniq(uniqPerson.get(i))){
				//then
				fail("Failed on Checking Uniq"+uniqPerson.get(i).getFirstAndScndName());
			}
		}
	}	
	@Test
	public void testAddLesson (){
		//given
		ArrayList <Lesson> testLessList = new ArrayList <Lesson> ();
		
		for (int i=0;i<11;i++){
			Auditorium testAudit = new Auditorium ((i+20),i);
			Group testGroup = new Group ("testLessonGroup"+i,i);
			Teacher testTeach = new Teacher (new PersonInfo("testLessonTeacher"+i,"Vladim","Kiev",16,11,1983));
			GregorianCalendar time = new GregorianCalendar(2014, 9, (i+1), 8, 45);
			Lesson testLess  = new Lesson ("testLesson"+i,testAudit,testGroup,testTeach,time);
			testLessList.add(testLess);
		}
		
		for (int i=0;i<testLessList.size();i++){
			//when
			if(!Base.addLesson(testLessList.get(i))){
				//then
				fail("Failed on adding new lessons");
			}
		}
		for (int i=0;i<testLessList.size();i++){
			//when
			if(Base.addLesson(testLessList.get(i))){
				//then
				fail("Failed on adding existing lessons");
			}
		}
		
	}
	@Test
	public void testGetGroupLessons (){
		//given
		ArrayList <Lesson> testLessList = new ArrayList <Lesson> ();
		Group testGroup = new Group ("testGroup",3);
		for (int i=0;i<11;i++){
			Auditorium testAudit = new Auditorium ((i+30),i);
			Teacher testTeach = new Teacher (new PersonInfo("testGetGroupLessonsTeacher"+i,"Vadim","Kiev",16,11,1983));
			GregorianCalendar time = new GregorianCalendar(2014, 10, (i+1), 8, 45);
			Lesson testLess  = new Lesson ("testLesson"+i,testAudit,testGroup,testTeach,time);
			testLessList.add(testLess);
			Base.addLesson(testLess);
		}
		//when
		ArrayList <Lesson> sortedLessons = Base.getGroupLessons(testGroup);
		
		for (int i=0;i<testLessList.size();i++){
			for (int j=0;j<sortedLessons.size();j++){
				if (testLessList.get(i).equals(sortedLessons.get(j))){
					break;
				}
				//then
				if (j == (sortedLessons.size()-1)){
					fail("Failed on checking"+testLessList.get(i).getLessonName());
				}
			}
		}
		
	}
}
